﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UnlessComment.Model;
using Web4Comment.Service;

namespace Web4Comment
{
    public partial class edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((String)Session["username"] == null || (String)Session["username"] == "")
            {
                Response.Redirect("./logon.aspx");
            }

            if (!IsPostBack)
            {
                UnlessComment.Model.UserTotal userTotal = new UnlessComment.Model.UserTotal();
                userTotal = Service.UserService.getUserTotal((String)Session["username"]);
                usernameTextBox.Text = userTotal.user.username;
                emailTextBox.Text = userTotal.user.email;
                maleRadioButton.Checked = userTotal.userInfo.sex == "1" ? true : false;
                //femaleRadioButton.Checked = userTotal.userInfo.sex == "0" ? true : false;
                locationTextBox.Text = userTotal.userInfo.location;
                hoppyTextBox.Text = userTotal.userInfo.screen_name;

                oldPasswordCheckLabel.Text = "请输入旧密码";
                passwordCheckLabel.Text = "请输入6~20位新密码";
                repeatPasswordCheckLabel.Text = "请重复新密码";
            }

        }

        protected void registerButton_Click(object sender, EventArgs e)
        {

            if (oldPasswordCheckLabel.Text != "" || passwordCheckLabel.Text != "" || repeatPasswordCheckLabel.Text != "")
            {

                //ClientScript.RegisterStartupScript(ClientScript.GetType(), "1", "<script>showDialog();</script>");
                //Page.RegisterStartupScript("", "<script>showDialog();</script>");
                Response.Write("<script language=javascript>alert(\"" + "修改失败！请根据提示修改" + "\");</script>");
                return;
            }

            UnlessComment.Model.User user = new UnlessComment.Model.User();

            user.username = usernameTextBox.Text.Trim();
            user.password = passwordTextBox.Text.Trim();
            user.email = emailTextBox.Text.Trim();

            UnlessComment.Model.UserInfo userinfo = new UnlessComment.Model.UserInfo();
            userinfo.sex = maleRadioButton.Checked ? "1" : "0";
            userinfo.location = locationTextBox.Text.Trim();
            userinfo.screen_name = hoppyTextBox.Text.Trim();

            //userService = new UserService();
            int exist = Service.UserService.update(user, userinfo);
            if (exist == 0)
            {
                Response.Write("<script language=javascript>alert(\"" + "修改失败！请重新尝试" + "\");</script>");
                return;
            }

            Response.Write("<script language=javascript>alert(\"" + "修改成功！" + "\");</script>");
            Response.Redirect("./logon.aspx");
        }

        protected void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
          

            if (passwordTextBox.Text == null || passwordTextBox.Text.Trim() == "")
            {
                passwordCheckLabel.Text = "密码不能为空";
            }
            else 
            {
                if (passwordTextBox.Text.Trim().Length < 6)
                {
                    passwordCheckLabel.Text = "密码不能少于6位";
                }
                else if (passwordTextBox.Text.Trim().Length > 20)
                {
                    passwordCheckLabel.Text = "密码不能多于20位";
                }
                else if (passwordTextBox.Text.Trim() == oldPasswordTextBox.Text.Trim())
                {
                    passwordCheckLabel.Text = "密码与旧密码相同";
                }
                else if (passwordTextBox.Text.Trim() == usernameTextBox.Text.Trim())
                {
                    passwordCheckLabel.Text = "用户名密码不能相同";
                }
                else
                {
                    passwordCheckLabel.Text = "";
                }
            }
        }


        protected void repeatPasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            if (repeatPasswordTextBox.Text.Trim() != passwordTextBox.Text.Trim())
            {
                repeatPasswordCheckLabel.Text = "两次密码输入不一致";
            }
            else
            {
                repeatPasswordCheckLabel.Text = "";
            }
        }

        protected void oldPasswordTextBox_TextChanged(object sender, EventArgs e)
        {
            if(Service.UserService.logon(usernameTextBox.Text.Trim(),oldPasswordTextBox.Text.Trim()) > 0)
            {
                oldPasswordCheckLabel.Text="";
            }
            else
            {
                oldPasswordCheckLabel.Text="密码不正确";
            }

            
        }


    }
}